package com.egao.sszg.biz.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.core.web.JsonResult;
import com.egao.common.system.entity.User;
import com.egao.sszg.biz.BillBiz;
import com.egao.sszg.entity.Bill;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据层实现类
 *
 * @author : Doman
 * @version ：1.0
 * @date : 2021-08-19 09:43:36
 */
@Repository
public class BillBizImpl extends BaseDao implements BillBiz {

  @Override
  @DS("slave_1")
  public JsonResult list(PageResult2<T> page, Bill bill, User loginUser) throws Exception {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<Bill> rm = new BeanPropertyRowMapper<Bill>(Bill.class);
    StringBuffer sql = new StringBuffer("");
    sql.append("select b.customer,b.bill_of_lading, df.faddress,df.fphone,b.driver,b.driver_phone,b.car_no,b.pack_date ");
    sql.append("from chf_bill b ");
    sql.append("inner join ( ");
    sql.append("select  df2.* from chf_address_factory df2 where df2.deleted=0 ");
    sql.append("and df2.id in ( ");
    sql.append("select min(df3.id) from chf_address_factory df3 where df3.deleted=0 group by bill_id ");
    sql.append(") ");
    sql.append(") df on df.bill_id=b.id and df.deleted=0 ");
    sql.append("where b.deleted=0 ");
    if (Utils.isNotEmpty(bill)) {
      if (Utils.isNotEmpty(bill.getDate1())) {
        sql.append(" and b.pack_date>= :date1 ");
        map.put("date1", bill.getDate1()+" 00:00:00");
      }
      if (Utils.isNotEmpty(bill.getDate2())) {
        sql.append(" and b.pack_date<=:date2 ");
        map.put("date2", bill.getDate2()+" 23:59:59");
      }
    }
    sql.append("order by b.driver ");
    return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
  }

  @Override
  public List<Bill> list(Bill bill, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    RowMapper<Bill> rm = new BeanPropertyRowMapper<Bill>(Bill.class);
    StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.customer_id");
    sql.append(",b.customer,b.bill_of_lading,b.faddress,b.fphone");
    sql.append(",b.driver,b.driver_phone,b.car_no,b.create_time");
    sql.append(",b.create_user,b.update_time,b.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from oa_bill b ");
    sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=b.organization_id ");
    sql.append(" where b.deleted=0 and sogzn.deleted=0 ");
    if (loginUser.getUserId() != UserConstant.ADMIN_ID) {
      sql.append(" and b.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if (Utils.isNotEmpty(bill)) {
//			if(Utils.isNotEmpty(bill.getName())){
//				sql.append(" and instr(b.name,:name)>0 ");
//				map.put("name", bill.getName());
//			}
      if (Utils.isNotEmpty(bill.getSort()) && Utils.isNotEmpty(bill.getOrder())) {
        sql.append(" order by b." + bill.getSort() + " " + bill.getOrder());
      } else {
        sql.append(" order by b.create_time desc");
      }
    }
    getMapParameter(sql, map);
    return jdbcTemplate.query(sql.toString(), map, rm);
  }

  @Override
  public Bill getById(Bill bill) {
    RowMapper<Bill> rm = new BeanPropertyRowMapper<Bill>(Bill.class);
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("select b.id,b.organization_id,b.customer_id");
    sql.append(",b.customer,b.bill_of_lading,b.faddress,b.fphone");
    sql.append(",b.driver,b.driver_phone,b.car_no,b.create_time");
    sql.append(",b.create_user,b.update_time,b.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.nick_name createName ");
    sql.append(" from oa_bill b ");
    sql.append(" inner join sys_user suser on suser.user_id=b.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=b.organization_id ");
    sql.append(" where b.deleted=0 and sogzn.deleted=0 and b.id=:id ");
    map.put("id", bill.getId());
    getMapParameter(sql, map);
    return jdbcTemplate.queryForObject(sql.toString(), map, rm);
  }

  @Override
  public boolean save(Bill bill, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("insert into oa_bill (");
    sql.append("organization_id,customer_id,customer,bill_of_lading");
    sql.append(",faddress,fphone,driver,driver_phone");
    sql.append(",car_no,create_user,create_time) values ( ");
    sql.append(":organization_id,:customer_id,:customer,:bill_of_lading");
    sql.append(",:faddress,:fphone,:driver,:driver_phone");
    sql.append(",:car_no,:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("customer_id", bill.getCustomerId());
    map.put("customer", bill.getCustomer());
    map.put("bill_of_lading", bill.getBillOfLading());
    map.put("faddress", bill.getFaddress());
    map.put("fphone", bill.getFphone());
    map.put("driver", bill.getDriver());
    map.put("driver_phone", bill.getDriverPhone());
    map.put("car_no", bill.getCarNo());
    map.put("create_user", loginUser.getUserId() + "");
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean update(Bill bill, User loginUser) {
    Map<String, String> map = new HashMap<String, String>();
    StringBuffer sql = new StringBuffer("update oa_bill set ");
    sql.append("customer_id=:customer_id,customer=:customer,bill_of_lading=:bill_of_lading ");
    sql.append(",faddress=:faddress,fphone=:fphone,driver=:driver ");
    sql.append(",driver_phone=:driver_phone,car_no=:car_no,update_time=now() ");
    sql.append(",update_user=:update_user where id=:id ");
    map.put("customer_id", bill.getCustomerId());
    map.put("customer", bill.getCustomer());
    map.put("bill_of_lading", bill.getBillOfLading());
    map.put("faddress", bill.getFaddress());
    map.put("fphone", bill.getFphone());
    map.put("driver", bill.getDriver());
    map.put("driver_phone", bill.getDriverPhone());
    map.put("car_no", bill.getCarNo());
    map.put("update_user", loginUser.getUserId() + "");
    map.put("id", bill.getId());
    getMapParameter(sql, map);
    int count = jdbcTemplate.update(sql.toString(), map);
    return count > 0;
  }

  @Override
  public boolean remove(Bill bill, User loginUser) {
    int count = 0;
    Map<String, String> map = null;
    if (bill.getId().length() > 0) {
      for (String ID : bill.getId().split(",")) {
        map = new HashMap<String, String>();
        if (ID == null || ID.equals("")) {
          break;
        }
        StringBuffer sql = new StringBuffer("update oa_bill set deleted=1" +
                ",update_user=:update_user, update_time=now() "
                + " where id =:id ");
        map.put("update_user", loginUser.getUserId() + "");
        map.put("id", ID);
        getMapParameter(sql, map);
        count = jdbcTemplate.update(sql.toString(), map);
      }
    }
    return count > 0;
  }
}